package com.common.excel.listener;

import com.common.base.DictionaryAware;
import com.common.constants.Constants;
import com.common.entity.department.DepartmentEntity;
import com.common.entity.personal.PersonalEntity;
import com.common.entity.standvalue.StandValueEntity;
import com.common.excel.ExcelListener;
import com.common.excel.ExcelValueCheckResp;
import com.common.excel.excelinfo.CaseExcelInfo;
import com.common.excel.excelinfo.TargetItemExcelInfo;
import com.common.repository.departmentjob.DepartmentRepository;
import com.common.repository.departmentjob.JobRepository;
import com.common.repository.judicialpersonal.PersonalRepository;
import com.common.repository.standvalue.StandValueRepository;
import com.common.repository.target.TargetItemRepository;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 版权片单 Excel Listener
 *
 * @author johnny
 * @create 2020-03-10 下午04:07
 **/
@Slf4j
@Getter
@Service
public class CaseExcelListener extends ExcelListener<CaseExcelInfo> {

    @Autowired
    private TargetItemRepository targetItemRepository;

    @Autowired
    private PersonalRepository personalRepository;

    @Autowired
    private JobRepository jobRepository;


    @Override
    protected boolean preProcess(CaseExcelInfo caseExcelInfo, Integer rowNumber, Map<Integer, List<ExcelValueCheckResp.ErrorInfoResp>> checkRespMap) {

        boolean pass = true;
        //校验personaName
        String personalName = caseExcelInfo.getPersonalName();

        PersonalEntity personalEntity = personalRepository.findByName(personalName);

        if (personalEntity == null) {
            putErrorInfo(rowNumber, new ExcelValueCheckResp.ErrorInfoResp("根据承办人[" + personalName + "] 找不到对应的个人档案"), checkRespMap);
            pass = false;
        } else {
            if (personalEntity.getDepartmentId() == null || personalEntity.getDepartmentId() == 0L) {
                putErrorInfo(rowNumber, new ExcelValueCheckResp.ErrorInfoResp("承办人[" + personalName + "] 未配置部门 请先配置"), checkRespMap);
                pass = false;
            } else {
                if (DictionaryAware.DEPARTMENT_MAP.get(personalEntity.getDepartmentId()) == null) {
                    putErrorInfo(rowNumber, new ExcelValueCheckResp.ErrorInfoResp("承办人[" + personalName + "] 配置的部门不存在 参考值:[" +
                            DictionaryAware.DEPARTMENT_MAP.values().stream().map(DepartmentEntity::getDepartmentName).reduce((v1, v2) -> v1 + Constants.COMMA + v2).orElse("") + "]"), checkRespMap);
                    pass = false;
                }
            }
            if (personalEntity.getJobId() == null || personalEntity.getJobId() == 0L) {
                putErrorInfo(rowNumber, new ExcelValueCheckResp.ErrorInfoResp("承办人[" + personalName + "] 未配置岗位 请先配置"), checkRespMap);
                pass = false;
            } else if (!DictionaryAware.JOB_MAP.containsKey(personalEntity.getJobId())) {
                putErrorInfo(rowNumber, new ExcelValueCheckResp.ErrorInfoResp("承办人[" + personalName + "] 配置的岗位 找不到 "), checkRespMap);
                pass = false;
            }
        }
        //校验 案件类别 caseType
        if (!DictionaryAware.CASE_TYPE_SET.contains(caseExcelInfo.getCaseType())) {
            putErrorInfo(rowNumber, new ExcelValueCheckResp.ErrorInfoResp("案件类别[" + caseExcelInfo.getCaseType() + "] 在字典中不存在 请先配置 参考值:[" +
                    DictionaryAware.CASE_TYPE_SET.stream().reduce((v1, v2) -> v1 + Constants.COMMA + v2).orElse("") + "]"), checkRespMap);
            pass = false;
        }
        //校验 承办单位 caseUnit
        if (!DictionaryAware.CASE_UNIT_SET.contains(caseExcelInfo.getCaseUnit())) {
            putErrorInfo(rowNumber, new ExcelValueCheckResp.ErrorInfoResp("承办单元[" + caseExcelInfo.getCaseUnit() + "] 在字典中不存在 请先配置 参考值:[" +
                    DictionaryAware.CASE_UNIT_SET.stream().reduce((v1, v2) -> v1 + Constants.COMMA + v2).orElse("") + "]"), checkRespMap);
            pass = false;
        }
        //校验 办案状态办案状态   采用字典 流程结束、裁判结果审查、裁判结果审查、全案不起诉
        if (!DictionaryAware.CASE_STATUS_SET.contains(caseExcelInfo.getCaseStatus())) {
            putErrorInfo(rowNumber, new ExcelValueCheckResp.ErrorInfoResp("办案状态[" + caseExcelInfo.getCaseStatus() + "] 在字典中不存在 请先配置 参考值:[" +
                    DictionaryAware.CASE_STATUS_SET.stream().reduce((v1, v2) -> v1 + Constants.COMMA + v2).orElse("") + "]"), checkRespMap);
            pass = false;
        }

        //校验 当前阶段 caseCurrentStage
        if (!DictionaryAware.CASE_CURRENT_STAGE_SET.contains(caseExcelInfo.getCaseStatus())) {
            putErrorInfo(rowNumber, new ExcelValueCheckResp.ErrorInfoResp("当前阶段[" + caseExcelInfo.getCaseStatus() + "] 在字典中不存在 请先配置 参考值:[" +
                    DictionaryAware.CASE_CURRENT_STAGE_SET.stream().reduce((v1, v2) -> v1 + Constants.COMMA + v2).orElse("") + "]"), checkRespMap);
            pass = false;
        }

        if (pass) {
            //填充属性
            caseExcelInfo.setPersonalId(personalEntity.getId());
            caseExcelInfo.setCasePersonalJob(DictionaryAware.JOB_MAP.get(personalEntity.getJobId()));
            caseExcelInfo.setCaseDepartmentId(personalEntity.getDepartmentId());
            caseExcelInfo.setCaseDepartmentName(DictionaryAware.DEPARTMENT_MAP.get(personalEntity.getDepartmentId()).getDepartmentName());
        }
        return pass;
    }
}




